﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities.Resumes;
using Forest.Core;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.Resumes
{
    public class SkillService : ISkillService
    {
        #region Constants
        private const string Skill_BY_ID_KEY = "skill.id-{0}";
        private const string Skill_PATTERN_KEY = "skill.";
        #endregion

        #region Fields
        private readonly IRepository<Skill> _skillRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenerateNumberService _generateNumberService;
        #endregion

        #region Ctor
        public SkillService(IRepository<Skill> workExperienceRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher, IGenerateNumberService generateNumberService)
        {
            _skillRepository = workExperienceRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
            _generateNumberService = generateNumberService;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteSkill(Skill skill)
        {
            if (skill == null)
                throw new ArgumentNullException("Skill");

            _skillRepository.Delete(skill);

            _cacheManager.RemoveByPattern(Skill_PATTERN_KEY);
            //event notification
            _eventPublisher.EntityDeleted(skill);

        }

        public Skill GetSkillById(Guid skillId)
        {
            if (skillId == Guid.Empty)
                return null;

            string key = string.Format(Skill_BY_ID_KEY, skillId);

            return _cacheManager.Get(key, () =>
            {
                var cl = _skillRepository.GetById(skillId);
                return cl;
            });
        }

        public virtual IPagedList<Skill> GetAllSkills(int pageIndex, int pageSize)
        {
            var query = _skillRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<Skill>(query, pageIndex, pageSize);
            return items;
        }

        public virtual IList<Skill> GetSkills(Guid resumeId)
        {
            var query = _skillRepository.Table;
            query = query.Where(c => c.ResumeId == resumeId).OrderByDescending(c => c.LastUpdated);

            return query.ToList();
        }

        public virtual void InsertSkill(Skill skill)
        {
            if (skill == null)
                throw new ArgumentNullException("Skill");

            _skillRepository.Insert(skill);

            _cacheManager.RemoveByPattern(Skill_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(skill);
        }

        public virtual void UpdateSkill(Skill skill)
        {
            if (skill == null)
                throw new ArgumentNullException("Skill");
            _skillRepository.Update(skill);

            _cacheManager.RemoveByPattern(Skill_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(skill);
        }
        #endregion

        public Guid CreateId
        {
            get
            {
                return _generateNumberService.GenerateId<Skill>(_skillRepository, GenerateType.Skill);
            }
        }

        public bool HasSkills(Guid resumeId)
        {
            return _skillRepository.Table.Any(s => s.ResumeId == resumeId);
        }
    }
}
